package com.pp.demo.common.base;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;
    private String msg;
    private T data;

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> BaseResult<T> success(T data) {
        return new BaseResult<>(0, "操作成功", data);
    }

    /**
     * 失败返回结果
     *
     * @param <T>
     * @return
     */
    public static <T> BaseResult<T> failed() {
        return failed(-1, "操作失败");
    }

    /**
     * 失败返回结果
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> BaseResult<T> failed(int code, String msg) {
        return new BaseResult<>(code, msg, null);
    }

    /**
     * 失败返回结果
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> BaseResult<T> failed(String msg) {
        return new BaseResult<>(-1, msg, null);
    }

}
